package cn.gok.server.controller;

import cn.gok.common.config.SystemLog;
import cn.gok.common.entity.backstage.Article;
import cn.gok.common.resp.PageResult;
import cn.gok.common.resp.Result;
import cn.gok.server.service.ArticleService;
import cn.gok.utils.ResultUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author: DxlinY
 * @apiNote: 商城资讯控制层
 * @date: 2020/9/27
 * @time: 11:24
 */
@RestController
@RequestMapping("article")
public class ArticleController {

    @Autowired
    private ArticleService articleService;


    /**
     * 商城资讯分页
     *
     * @param page  当前页
     * @param limit 页容量
     * @return result
     */
    @RequestMapping(value = "gArticlePageList", method = RequestMethod.POST)
    @SystemLog(model = "商城资讯", method = "分页")
    public PageResult pageList(int page, int limit, Article article) {
        return ResultUtil.pageInfo(articleService.listAll(page, limit, article));
    }

    /**
     * 提交插入一个资讯
     *
     * @param article 资讯对象
     * @return result
     */
    @RequestMapping(value = "/articleAdd", method = RequestMethod.POST)
    @SystemLog(model = "商城资讯", method = "插入资讯")
    public Result addArticle(Article article, HttpServletRequest res) {
        if (articleService.addGArticle(article) > 0) {
            return ResultUtil.success(null);
        }
        return ResultUtil.error(null);
    }


    /**
     * 批量删除资讯（一条也行）
     * <p>
     * //     * @param ids 编号
     *
     * @return result
     */
    @RequestMapping(value = "/delByIds", method = RequestMethod.POST)
    @SystemLog(model = "商城资讯", method = "删除资讯")
    public Result delArticleByIds(HttpServletRequest request) {
        String[] idsStr = request.getParameterValues("ids");
        List <Long> list = new ArrayList <>();
        for (String id : idsStr) {
            list.add(Long.parseLong(id));
        }
        if (articleService.delByIds(list) > 0) {
            return ResultUtil.success(null);
        }
        return ResultUtil.error(null);
    }

    /**
     * 获取编号为articleId的资讯信息
     *
     * @param articleId 资讯编号
     * @return result
     */
    @RequestMapping(value = "/getArticleById", method = RequestMethod.POST)
    @SystemLog(model = "商城资讯", method = "获取资讯信息")
    public Result getArticleById(int articleId) {
        return ResultUtil.success(articleService.getById(articleId));
    }


    /**
     * 更新资讯
     *
     * @param article 资讯
     * @return result
     */
    @RequestMapping(value = "/updateArticleById", method = RequestMethod.POST)
    @SystemLog(model = "商城资讯", method = "更新资讯")
    public Result updateArticleById(Article article) {
        if (articleService.updateById(article) > 0) {
            ResultUtil.success(null);
        }
        return ResultUtil.error(null);
    }

    /**
     * 修改资讯发布状态
     *
     * @param articleId 资讯编号
     * @param isEnable  发布状态
     * @return result
     */
    @RequestMapping(value = "/updateIsEnableById", method = RequestMethod.POST)
    @SystemLog(model = "商城资讯", method = "更改发布状态")
    public Result updateIsEnableById(long articleId, boolean isEnable) {
        if (articleService.updateIsEnableById(articleId, isEnable) > 0) {
            ResultUtil.success(null);
        }
        return ResultUtil.error(null);
    }
}
